package org.ielts.action.base;

import static com.yifeng.struts2.RequestParameters.*;
import static com.yifeng.struts2.ResultParameters.*;
import static com.yifeng.struts2.ResultConstants.*;
import static com.yifeng.struts2.ActionUtils.*;

import java.util.Collection;

import org.ielts.action.BaseAction;
import org.ielts.domain.base.Course;
import org.ielts.domain.base.CourseType;
import org.ielts.service.CourseService;
import org.ielts.service.CourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yifeng.modules.annotations.Operation;
import com.yifeng.utils.JsonResultBuilder;

public class CourseTypeAction extends BaseAction {

	@Autowired
	CourseService courseService;

	@Autowired
	CourseTypeService courseTypeService;

	@Operation(result = FILL)
	public String selectCourse() {
		String courseTypeId = form.readString("courseTypeId");
		CourseType courseType = courseTypeService.findOne(courseTypeId);

		Iterable<Course> courses = courseService.findAll();

		put("courses", courses);
		put("courseType", courseType);

		return FILL;
	}

	@Operation(result = JSON)
	public String doSetCourseInType() {
		String[] courseIds = form.readStrings("courseId");
		String courseTypeId = form.readString("typeId");

		courseTypeService.setCourseInType(courseTypeId, courseIds);

		setJson(JsonResultBuilder.success(true).msg("修改下属课程成功").json());
		return JSON;
	}

	@Operation(result = JSON)
	public String doCreateCourseType() {

		String name = form.readString("name");
		String title = form.readString("title");
		String cssClass = form.readString("cssClass");

		courseTypeService.createCourseType(name, title, cssClass);

		setJson(JsonResultBuilder.success(true).msg("保存成功").json());
		return JSON;
	}

	@Operation(result = FILL)
	public String modifyCourseType() {
		String id = form.readString("id");

		CourseType courseType = courseTypeService.findOne(id);

		put("courseType", courseType);

		return FILL;
	}

	@Operation(result = FILL)
	public String doModifyCourseType() {
		String id = form.readString("id");
		String name = form.readString("name");
		String title = form.readString("title");
		String cssClass = form.readString("cssClass");

		CourseType courseType = courseTypeService.modifyCourseType(id, name, title, cssClass);

		setJson(JsonResultBuilder.success(true).msg("保存成功").json());
		return JSON;
	}

	@Operation(result = FILL)
	public String doRemoveCourseType() {
		String id = form.readString("id");

		courseTypeService.removeCourseType(id);

		setJson(JsonResultBuilder.success(true).msg("删除成功").json());
		return JSON;
	}

	@Operation(result = FILL)
	public String listCourseType() {

		Iterable<CourseType> courseTypes = courseTypeService.findAll();
		put("courseTypes", courseTypes);

		return FILL;
	}

	public String[] getColors() {
		return colors;
	}

	public void setColors(String[] colors) {
		this.colors = colors;
	}

	private String[] colors = new String[] { "blue-bg", "blueDark-bg", "green-bg", "red-bg", "yellow-bg", "orange-bg",
			"pink-bg", "purple-bg" };
}
